package com.syp.hehe.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.syp.hehe.domain.vo.UserVo;
import com.syp.hehe.service.UserServiceI;
import com.syp.hehe.util.ProcessResult;

@Controller
@RequestMapping({ "/user" })
public class UserController {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserServiceI userService;

	@RequestMapping({ "/dsignup" })
	public ModelAndView dsignup(UserVo user) {
		ProcessResult pr = new ProcessResult();
		ModelAndView mv = null;
		try {
			this.userService.dsignup(user);

			mv = new ModelAndView("/user/signup.result");
			mv.addObject("result", pr);
			pr.setSuccess(true);
			pr.setMessage("用户注册成功。");
		} catch (Exception e) {
			logger.error("用户注册失败。", e);

			pr.setSuccess(false);
			pr.setMessage("用户注册失败。");
			mv = new ModelAndView("/system/500");
			mv.addObject("result", pr);
		}

		return mv;
	}

	@RequestMapping({ "/dsignin" })
	@ResponseBody
	public ProcessResult dsignin(UserVo user, HttpServletRequest request) {
		ProcessResult pr = new ProcessResult();
		try {
			boolean success = this.userService.dsignin(user);
			if (success) {
				pr.setSuccess(true);
				pr.setMessage("用户登录成功。");
			} else {
				pr.setSuccess(false);
				pr.setMessage("用户登录失败。");
			}
		} catch (Exception e) {
			logger.error("用户注册失败。", e);

			pr.setSuccess(false);
			pr.setMessage("用户登录失败");
		}

		return pr;
	}
}
